package com.six.apidemic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.six.apidemic.R;
import com.six.apidemic.bean.ScanHistoryBean;
import com.six.apidemic.databinding.ItemScanHistoryBinding;
import com.six.apidemic.listener.BaseRecyclerViewHolder;
import com.six.apidemic.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class ScanHistoryAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
   private List<ScanHistoryBean.ResultBean> strings = new ArrayList<>();
   private Context mcontext;
   public OnItemClickListener mOnItemClickLitener;

   public ScanHistoryAdapter(Context context, List<ScanHistoryBean.ResultBean> strings) {
      this.mcontext = context;
      this.strings = strings;
   }

   @Override
   public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      ItemScanHistoryBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
              R.layout.item_scan_history, parent, false);
      binding.setAdapter(this);
      return new TourViewHolder(binding);
   }

   @Override
   public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
      holder.bindData(strings.get(position));
      TourViewHolder tv = (TourViewHolder) holder;

      if (mOnItemClickLitener != null) {
         holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               int pos = holder.getLayoutPosition();
               mOnItemClickLitener.onItemClick(holder.itemView, pos);
            }
         });

         holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
               int pos = holder.getLayoutPosition();
               mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
               return false;
            }
         });
      }
   }

   public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
      this.mOnItemClickLitener = mOnItemClickLitener;
   }

   @Override
   public int getItemCount() {
      return strings.size();
   }

   public class TourViewHolder extends BaseRecyclerViewHolder<ScanHistoryBean.ResultBean> {
      ItemScanHistoryBinding binding;

      public TourViewHolder(ItemScanHistoryBinding dataBinding) {
         super(dataBinding.getRoot());
         binding = dataBinding;
      }

      @Override
      public void bindData(ScanHistoryBean.ResultBean data) {
         binding.setBean(data);
      }
   }

   @Override
   public int getItemViewType(int position) {
      return strings.size();
   }

}